# Copyright (C) 2009-2011 David Zanetti
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; version 2 of the License.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Main project Makefile

CC=/usr/bin/avr-gcc
SVNDEF := -D'SVNVER="$(shell svnversion -n .)"'
DATEDEF := -D'BUILDDATE="$(shell date +%Y%m%d)"'
# DEBUG_SPI DEBUG_CLOCK DEBUG_ENC28J60
#DEBUG := -DDEBUG_CLOCK
#DEBUG := -DDEBUG_ENCX24J600
#DEBUG += -DDEBUG_ETHER
#DEBUG += -DDEBUG_ARP
#DEBUG += -DDEBUG_ARP_ROUTING
#DEBUG += -DDEBUG_ENCX24J600_SLOT
#DEBUG += -DDEBUG_ICMP
#DEBUG += -DDEBUG_IP
#DEBUG += -DDEBUG_UDP
#DEBUG += -DDEBUG_UDP_SEND
#DEBUG += -DDEBUG_DHCPC
DEBUG += -DDEBUG_MDNS
CFLAGS= $(DEBUG) $(SVNDEF) $(DATEDEF) --std=c99 -O2 -funroll-loops -Wa,-adhlns=$(<:.c=.lst) -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes -Wall -mcall-prologues -mmcu=atmega1284p -I. -g
OBJ2HEX=/usr/bin/objcopy
TARGET=ntpserver
AVRFLAGS = -p m1284p -c dragon_jtag -P usb:95:78
AVRDUDE = /usr/bin/avrdude $(AVRFLAGS)
#AVRFLAGS = -g -j usb -p atmega1284p 
#AVRDUDE = avarice $(AVRFLAGS)

# objects for various subsystems 
# core
OBJ += ntpserver.o led.o clock.o
# serial and consoles
OBJ += serial.o console.o ringbuffer.o
# GPS
OBJ += venus634.o
# flash
OBJ += spi.o at25df.o
# ethernet/ip
OBJ += encx24j600.o ether.o arp.o ip.o udp.o checksum.o icmp.o dhcpc.o mdns.o

#LDFLAGS = -Wl,-Map=$(TARGET).map -Wl,--cref -Wl,-u,vfprintf -lprintf_flt -lm
LDFLAGS = -Wl,-Map=$(TARGET).map -Wl,--cref -lm

all : $(TARGET).hex $(TARGET).eep

program : $(TARGET).hex $(TARGET).eep
	$(AVRDUDE) -U flash:w:$(TARGET).hex

eeprom : $(TARGET).eep
	$(AVRDUDE) -U eeprom:w:$(TARGET).eep

%.o : %.c %.h global.h Makefile
	$(CC) -c $(CFLAGS) $< -o $@

#%.obj : %.o
#	$(CC) $(CFLAGS) $< -o $@

.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf : $(OBJ)
	$(CC) $(CFLAGS) $(OBJ) --output $@ $(LDFLAGS)

%.hex : %.elf
	$(OBJ2HEX) -R .eeprom -O ihex $< $@

%.eep : %.elf
	$(OBJ2HEX) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
	--change-section-lma .eeprom=0 -O ihex $< $@

clean :
	rm -f *.hex *.elf *.eep *.o *.map *.lst

reset :
	$(AVRDUDE) 
